package fr.inpl_nancy.mines.andromines.envemu;

import java.io.*;
import java.net.*;

import fr.inpl_nancy.mines.andromines.bluetooth.Bluetooth;

/**
 * Connection (between JANE and the bluetooth/geoloc/other emulator) handler
 * 
 * @author Olivier Genest
 * @version 0.1
 * 
 */

public class EnvironmentEmulationConnectionHandler extends Thread {
		private Socket connexionSocket;
		
		public EnvironmentEmulationConnectionHandler(Socket s) {
			connexionSocket = s;
		}
		
		public void run() {
			
			try {
				//Establishing connection
				PrintStream sortie = new PrintStream(connexionSocket.getOutputStream());
				BufferedReader entree = new BufferedReader(new InputStreamReader(connexionSocket.getInputStream()));
			
				
				//Getting the request
				String requete = entree.readLine();
				
				//getting the upperLayer
				//TODO : parsing the xml data to know the concerned upper layer
				//xml should look like this <data type="bluetooth/geoloc/other">...</data>
				EnvironmentEmulationInterface upperLayer = EnvironmentEmulation.getEnvironmentEmulation().getUpperLayer("bluetooth");
				
				//treating the request
				upperLayer.treat(requete);
				
				//Closing the connection
				connexionSocket.close();
				
				
			} catch(IOException e) {}
		}

}
